/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    can.h
  * @brief   This file contains all the function prototypes for
  *          the can.c file
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CAN_H__
#define __CAN_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/

/* USER CODE BEGIN Includes */
#include "stm32f1xx_hal_can.h"
/* USER CODE END Includes */


/* USER CODE BEGIN Private defines */
#define CAN_RX_PORT GPIOA 
#define CAN_RX_PIN GPIO_PIN_11
#define CAN_TX_PORT GPIOA  
#define CAN_TX_PIN GPIO_PIN_12 

#define CAN_TXHEADER_IDE CAN_ID_STD
#define CAN_RX0_INT_ENABLE 1

#if CAN_TXHEADER_IDE == CAN_ID_STD
  #define CAN_FILTER_SHFT 21 
#else 
  #define CANFILTER_SHFT 3
#endif 
/* USER CODE END Private defines */
#define CAN_RX_IRQn               USB_LP_CAN1_RX0_IRQn
#define CAN_RX_IRQHandler         USB_LP_CAN1_RX0_IRQHandler
#define CAN_RX_Priority           10                    

void MX_CAN_Init(void);

/* USER CODE BEGIN Prototypes */
typedef struct{
  CAN_TxHeaderTypeDef header;
  uint8_t msg[8];
}CAN_TxHeaderWithMsgDef;

typedef struct{
  CAN_RxHeaderTypeDef header;
  uint8_t msg[8];
}CAN_RxHeaderWithMsgDef;

void printCANTxMsg(CAN_TxHeaderWithMsgDef *txmsg);
void printCANRxMsg(CAN_RxHeaderWithMsgDef *rxmsg);

uint8_t can_init(uint32_t tsjw, uint32_t tbs2, uint32_t tbs1, uint16_t brp, uint32_t mode);
uint32_t can_filter_config(uint32_t filterId, uint32_t filterMask);
uint8_t can_send_msg(uint32_t id, uint8_t *msg, uint8_t len, uint32_t delay);
uint8_t can_receive_msg(uint8_t *buf);
uint8_t can_receive(CAN_HandleTypeDef *canx, uint32_t fifo, CAN_RxHeaderWithMsgDef *rxmsg);
void TestCANProcess(const void* arg); /* CAN双板通信测试例程 */

/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __CAN_H__ */

